package com.tem.system.controller;

import cn.hutool.core.bean.BeanUtil;
import com.api.manage.pojo.system.SysDepartmentBean;
import com.tem.common.core.base.BaseController;
import com.tem.common.tool.result.ResultUtil;
import com.tem.system.entity.SysDepartmentEntity;
import com.tem.system.service.SysDeptService;
import com.tem.system.vo.SysDeptRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 腾木科技
 * @date 2021/6/13 12:02
 */
@Api(tags = "系统部门信息API")
@RestController
@RequestMapping("/dept")
public class SysDeptController extends BaseController {

    private final SysDeptService sysDeptService;

    public SysDeptController(SysDeptService sysDeptService) {
        this.sysDeptService = sysDeptService;
    }

    @ApiOperation(value = "查询部门树列表")
    @GetMapping("/list")
    public ResultUtil<List<SysDepartmentEntity>> list(SysDeptRequest request, HttpServletRequest req){
        if(request.getSysBranchOfficeId()==null){
            request.setSysBranchOfficeId(this.getSysBranchOfficeId(req));
        }
        return sysDeptService.queryList(request);
    }
    @ApiOperation(value = "查询部门树列表--下拉框选择")
    @GetMapping("/select")
    public ResultUtil<List<SysDepartmentEntity>> select(SysDeptRequest request, HttpServletRequest req){
        if(request.getSysBranchOfficeId()==null) request.setSysBranchOfficeId(this.getSysBranchOfficeId(req));
        return sysDeptService.queryList(request);
    }
    @ApiOperation(value = "添加部门")
    @PostMapping("/save")
    public ResultUtil<SysDepartmentEntity> generateId(@RequestBody SysDepartmentEntity sysDepartmentEntity, HttpServletRequest req) {
        if(sysDepartmentEntity.getSysBranchOfficeId()==null){
            sysDepartmentEntity.setSysBranchOfficeId(this.getSysBranchOfficeId(req));
        }
        return sysDeptService.saveDept(sysDepartmentEntity);
    }

    @ApiOperation(value = "批量删除部门")
    @DeleteMapping("/bath_delete")
    public ResultUtil<?> deleteBath(@RequestBody long[] ids) {
        return sysDeptService.deleteBath(ids);
    }

    @GetMapping("/getById/{id}")
    @ApiOperation(value = "根据id查询部门")
    public ResultUtil<SysDepartmentEntity> getById(@PathVariable String id){
        return sysDeptService.getDeptById(id);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除系统部门")
    public ResultUtil<SysDepartmentEntity> delete(@PathVariable String id){
        return sysDeptService.deleteDept(id);
    }

    @GetMapping("/getSysDepartmentBeanById{id}")
    public ResultUtil<SysDepartmentBean> getSysDepartmentBeanById(@PathVariable("id") Long sysBranchOfficeId){
        SysDepartmentEntity sde = sysDeptService.getById(sysBranchOfficeId);
        SysDepartmentBean bean = new SysDepartmentBean();
        BeanUtil.copyProperties(sde, bean);
        return ResultUtil.success(bean);
    }
}
